package com.scott.crm.clazz.controller;

import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.crm.clazz.bean.ClazzBean;
import com.scott.crm.clazz.bean.ClazzStudentTeacherBean;
import com.scott.crm.clazz.dto.ClazzDto;
import com.scott.crm.clazz.service.ClazzService;
import com.scott.crm.clazz.service.ClazzStudentTeacherService;
import com.scott.crm.teacher.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 方法描述:
 * @author: 李伟伟
 * @date: 2021/11/30 22:09
 */
@CrossOrigin
@RestController
@Slf4j
public class ClazzController {

    @Autowired
    private ClazzService service;

    @Autowired
    private ClazzStudentTeacherService clazzStudentTeacherService;

    @Autowired
    private TeacherService teacherService;

    /*
     * 方法描述: 班级添加
     * @author: 李伟伟
     * @date: 2021/12/2 11:12
     * @params: [clazzBean]
     * @returns:com.scott.base.message.DataRes
     */
    // http:localhost:8080/clazz/add
    @RequestMapping(value = "clazz/add", method = RequestMethod.POST)
    @CrossOrigin
    public DataRes addClazz(@RequestBody ClazzBean clazzBean) {
        if(clazzBean==null){
            return null;
        }
        service.addClazz(clazzBean);
        return DataUtil.success(clazzBean);
    }

    /*
     * 方法描述: 班级批量删除 (未实现)
     * @author: 李伟伟
     * @date: 2021/11/30 22:00
     * @params: [ids]
     * @returns:com.scott.base.message.DataRes
     */
    // http:localhost:8080/clazz/delete
    @RequestMapping(value = "clazz/delete", method = RequestMethod.POST)
    @CrossOrigin
    public DataRes deleteClazz(@RequestBody ClazzDto clazzDto) throws Exception{
        Integer integer = service.deleteClazz(clazzDto);

        // return DataUtil.success( service.deleteClazz(clazzDto));
        return DataUtil.success(integer);
    }

    /*
     * 方法描述:班级修改
     * @author: 李伟伟
     * @date: 2021/11/30 22:15
     * @params: [clazzBean]
     * @returns:com.scott.base.message.DataRes
     */
    // http:localhost:8080/clazz/update
    @RequestMapping(value = "clazz/update", method = RequestMethod.POST)
    @CrossOrigin
    public DataRes updateClazz(@RequestBody ClazzBean clazzBean) {

        Integer integer = service.updateClazzByClazzId(clazzBean);
        // return 返回修改后结果
        return DataUtil.success(integer);
    }

    /*
     * 方法描述: 班级显示
     * @author: 李伟伟
     * @date:  2021/11/30 22:45
     * @params: []
     * @returns:com.scott.base.message.DataRes
     */
    // http:localhost:8080/clazz/select
    @RequestMapping(value = "clazz/select", method = RequestMethod.GET)
    @CrossOrigin
    public DataRes selectAllClazz() {
        List<ClazzBean> clazzBeans = service.selectAllClazz();
        return DataUtil.success(clazzBeans);
    }

    // http:localhost:8080/clazz/selectById
    @RequestMapping(value = "clazz/selectMessageById", method = RequestMethod.GET)
    @CrossOrigin
    public DataRes selectMessageById(Integer id) {
        service.selectMessageById(id);
        return DataUtil.success("通过id看信息");
    }


    /*
     * 方法描述: 列表分页
     * @author: 李伟伟
     * @date: 2021/11/30 23:09
     * @params: [pageNumber, pageSize]
     * @returns:com.scott.base.message.DataRes
     */
    // http:localhost:8080/clazz/list
    @CrossOrigin
    @RequestMapping(value = "clazz/list", method = RequestMethod.GET)
    public DataRes list(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) throws Exception {
        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if (pageNumber < 1) {
            pageNumber = 1;
        }
        Integer begin = (pageNumber - 1) * pageSize;
        Integer end = pageSize;

        List<ClazzBean> clazzList = service.findClazzList(begin, end);

        Map<String, Object> rs = new HashMap<>();
        rs.put("clazzList", clazzList);

        //pageNumber 第几页
        rs.put("pageNumber", pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize", pageSize);

        List<ClazzBean> clazzBeans = service.selectAllClazz();
        if (!CollectionUtils.isEmpty(clazzBeans)) {
            Integer totalCounter = clazzBeans.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter", totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter % pageSize == 0 ? totalCounter / pageSize : totalCounter / pageSize + 1;
            rs.put("totalPage", totalPage);

        }
        log.info("clazzBeans=" + clazzBeans.toString());
        return DataUtil.success(rs);
    }

    @RequestMapping(value = "/clazz/findAll",method = RequestMethod.GET)
    public DataRes findAll(@RequestParam(value="pageNumber",defaultValue = "1") Integer pageNumber,
                           @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize) throws Exception{
        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if(pageNumber<1){
            pageNumber=1;
        }
        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        List<ClazzStudentTeacherBean> userRoles = clazzStudentTeacherService.findClazzStudentLimit(begin,end);

        Map<String,Object> rs= new HashMap<>();
        rs.put("clazzStudents",userRoles);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<ClazzStudentTeacherBean> userRoleBeans = clazzStudentTeacherService.findClazzStudent();
        List<ClazzBean> allRoles = clazzStudentTeacherService.findClazzList();
        rs.put("clazzs",allRoles);
        if(!CollectionUtils.isEmpty(userRoleBeans)){
            Integer totalCounter = userRoleBeans.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }
        log.info("roleList="+userRoles.toString());
        return DataUtil.success(rs);
    }

    @RequestMapping(value = "/assign/clazz/student",method = RequestMethod.POST)
    public DataRes assignClazz(@RequestBody ClazzStudentTeacherBean clazzStudentTeacherBean) throws Exception{
        Integer addTeacher = clazzStudentTeacherService.addStudent(clazzStudentTeacherBean);
        if(addTeacher!=0){
            return DataUtil.success(addTeacher);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "delete/clazz/student",method = RequestMethod.POST)
    public DataRes deleteAssignClazz(@RequestBody Map<String,List<Integer>> map) throws Exception{


        if(CollectionUtils.isEmpty(map)||map.get("ids").isEmpty()){
            return DataUtil.errors();
        }

        List<Integer> ids = map.get("ids");


        int counter = teacherService.deleteAssignClazz(ids);
        if(counter >0){
            return DataUtil.success(counter);
        }else{
            return DataUtil.errors();
        }
    }

}